﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;
using System.Diagnostics;


namespace DAL
{
    public class OutNewDataAccess : FreshCmsAccessBase
    {
        #region code CMS

        public List<OutNew> GetOutNewListByPaging(bool isParent, string title, DateTime from, DateTime to, int cate, int stt, int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                string spName = "";
                if (isParent)
                    spName = "spi_OutNew_GetListSearchByParentId";
                else
                    spName = "spi_OutNew_GetListSearch";

                var sqlParams = new[]
                                    {
                                        new SqlParameter("@NewTitle", title),
                                        new SqlParameter("@NewPublishDate", from),
                                        new SqlParameter("@NewPublishDate1", to),
                                        new SqlParameter("@CatId", cate),
                                        new SqlParameter("@NewStatus", stt),
                                        new SqlParameter("@PageIndex", pageIndex),
                                        new SqlParameter("@PageSize", pageSize),
                                        new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var outNewList = DbHelper.ExecuteSqlList<OutNew>(sqlCommand);
                if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int)sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return outNewList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<OutNew>();
            }
        }

        /// <summary>
        /// Hàm INSERT dành cho mọi tài khoản
        /// </summary>
        /// <param name="OutNew"></param>
        /// <returns></returns>
        public int InsertOutNew(OutNew OutNew)
        {
            try
            {
                const string spName = "spi_OutNew_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@CatId", OutNew.CatId),
										new SqlParameter("@NewAuthor", OutNew.NewAuthor),
										new SqlParameter("@NewTitle", OutNew.NewTitle),
										new SqlParameter("@NewDesc", OutNew.NewDesc),
										new SqlParameter("@NewDetail", OutNew.NewDetail),
										new SqlParameter("@NewImage", OutNew.NewImage),
										new SqlParameter("@NewImage2", OutNew.NewImage2),
										new SqlParameter("@NewImage3", OutNew.NewImage3),
										new SqlParameter("@NewStatus", OutNew.NewStatus),
										new SqlParameter("@NewPublishDate", OutNew.NewPublishDate),
										new SqlParameter("@UserId", OutNew.UserId),
                                        new SqlParameter("@NewVideo", OutNew.NewVideo),
										new SqlParameter("@NewPhoto", OutNew.NewPhoto),
										new SqlParameter("@NewHot", OutNew.NewHot),
										new SqlParameter("@NewTop", OutNew.NewTop),
										new SqlParameter("@NewId", OutNew.NewId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@NewId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@NewId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@NewId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Hàm update dành cho User, update check theo UserID, đúng UserID tạo ra mới được sửa
        /// </summary>
        /// <param name="OutNew"></param>
        /// <returns></returns>
        public int UpdateOutNewWidthRole(OutNew OutNew)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutNew_Update_CheckRole";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@NewId", OutNew.NewId),
                                        new SqlParameter("@CatId", OutNew.CatId),
										new SqlParameter("@NewAuthor", OutNew.NewAuthor),
										new SqlParameter("@NewTitle", OutNew.NewTitle),
										new SqlParameter("@NewDesc", OutNew.NewDesc),
										new SqlParameter("@NewDetail", OutNew.NewDetail),
										new SqlParameter("@NewImage", OutNew.NewImage),
										new SqlParameter("@NewImage2", OutNew.NewImage2),
										new SqlParameter("@NewImage3", OutNew.NewImage3),
										new SqlParameter("@NewStatus", OutNew.NewStatus),
										new SqlParameter("@NewPublishDate", OutNew.NewPublishDate),
										new SqlParameter("@UserId", OutNew.UserId),
                                        new SqlParameter("@NewVideo", OutNew.NewVideo),
										new SqlParameter("@NewPhoto", OutNew.NewPhoto),
										new SqlParameter("@NewHot", OutNew.NewHot),
										new SqlParameter("@NewTop", OutNew.NewTop),
                                        new SqlParameter("@ReturnValue", returnVal),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Hàm update dành cho Admin, update không check theo UserID
        /// </summary>
        /// <param name="OutNew"></param>
        /// <returns></returns>
        public int UpdateOutNew(OutNew OutNew)
        {
            try
            {
                const string spName = "spi_OutNew_Update";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@NewId", OutNew.NewId),
                                        new SqlParameter("@CatId", OutNew.CatId),
										new SqlParameter("@NewAuthor", OutNew.NewAuthor),
										new SqlParameter("@NewTitle", OutNew.NewTitle),
										new SqlParameter("@NewDesc", OutNew.NewDesc),
										new SqlParameter("@NewDetail", OutNew.NewDetail),
										new SqlParameter("@NewImage", OutNew.NewImage),
										new SqlParameter("@NewImage2", OutNew.NewImage2),
										new SqlParameter("@NewImage3", OutNew.NewImage3),
										new SqlParameter("@NewStatus", OutNew.NewStatus),
										new SqlParameter("@NewPublishDate", OutNew.NewPublishDate),
                                        new SqlParameter("@NewVideo", OutNew.NewVideo),
										new SqlParameter("@NewPhoto", OutNew.NewPhoto),
										new SqlParameter("@NewHot", OutNew.NewHot),
										new SqlParameter("@NewTop", OutNew.NewTop),
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        
        /// <summary>
        /// Get chi tiết tin cho CMS
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public OutNew GetOutNewById(int Id)
        {
            try
            {
                const string spName = "spi_OutNew_GetById";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@NewId", Id)
                                    };

                var OutNew = DbHelper.ExecuteSPList<OutNew>(spName, sqlParams).SingleOrDefault();
                return OutNew;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }

        /// <summary>
        /// Hàm UPDATE FAST cho Super Admin
        /// </summary>
        /// <param name="OutNew"></param>
        /// <returns></returns>
        public int UpdateFastOutNew(OutNew OutNew)
        {
            try
            {
                const string spName = "spi_OutNew_UpdateFast";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@NewId", OutNew.NewId)
                                        , new SqlParameter("@NewTitle", OutNew.NewTitle)
                                        , new SqlParameter("@NewStatus", OutNew.NewStatus)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }


        /// <summary>
        /// Hàm update Fast dành cho User, update check theo UserID, đúng UserID tạo ra mới được sửa
        /// </summary>
        /// <param name="OutNew"></param>
        /// <returns></returns>
        public int UpdateFastOutNewWidthRole(OutNew OutNew)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutNew_UpdateFast_CheckRole";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@NewId", OutNew.NewId),
                                        new SqlParameter("@NewTitle", OutNew.NewTitle),
                                        new SqlParameter("@NewStatus", OutNew.NewStatus),
                                        new SqlParameter("@UserId", OutNew.UserId),
                                        new SqlParameter("@ReturnValue", returnVal),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Update status tin với Super ADMIN (dùng cho xóa tạm thời và phục hồi)
        /// </summary>
        /// <param name="newId"></param>
        /// <returns></returns>
        public int UpdateOutNewStatus(int newId, int stt)
        {
            try
            {
                const string spName = "spi_OutNew_UpdateStatus";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@NewId", newId)
                                    , new SqlParameter("@NewStatus", stt)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Update status tin có check đúng tài khoản mới update (dùng cho xóa tạm thời và phục hồi)
        /// </summary>
        /// <param name="newId"></param>
        /// <returns></returns>
        public int UpdateOutNewStatusWidthRole(int newId, int stt, int userid)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutNew_UpdateStatus_CheckRole";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@NewId", newId),
                                    new SqlParameter("@NewStatus", stt),
                                    new SqlParameter("@UserId", userid),
                                    new SqlParameter("@ReturnValue", returnVal),
                                };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Delete tin cho Super ADMIN (dùng cho xóa vĩnh viễn)
        /// </summary>
        /// <param name="newId"></param>
        /// <returns></returns>
        public int DeleteOutNew(int newId)
        {
            try
            {
                const string spName = "spi_OutNew_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@NewId", newId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Delete tin có check đúng tài khoản mới Delete (dùng cho xóa vĩnh viễn)
        /// </summary>
        /// <param name="newId"></param>
        /// <returns></returns>
        public int DeleteOutNewWidthRole(int newId, int userid)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutNew_Delete_CheckRole";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@NewId", newId),
                                    new SqlParameter("@UserId", userid),
                                    new SqlParameter("@ReturnValue", returnVal),
                                };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Check xem bai viet co dung theo ng viet hay khong, tra ve > 0 la ok
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public int CheckRole(int userid, int newid)
        {
            try
            {
                int temp = 0;
                const string spName = "spi_OutNew_CheckRole";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserId", userid),
                                        new SqlParameter("@NewId", newid),
                                        new SqlParameter("@Return", temp),
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@Return"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@Return"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@Return"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

		#endregion
    }
}
